package myapp.framelibrary.skin;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;

import java.lang.reflect.Method;

/**
 * Created by 超 on 2017/7/4.
 */

public class SkinResource {
    private Resources mResources;
    private String mPackageName;
    public SkinResource(Context context, String skinPath) {


        try {
            Resources resources = context.getResources();
            AssetManager assetManager = AssetManager.class.newInstance();
            Method addAssetPath = AssetManager.class.getDeclaredMethod("addAssetPath", String.class);
            addAssetPath.invoke(assetManager, skinPath);
            mResources = new Resources(assetManager, resources.getDisplayMetrics(), resources.getConfiguration());
            mPackageName = context.getPackageManager()
                    .getPackageArchiveInfo(skinPath, PackageManager.GET_ACTIVITIES).packageName;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public SkinResource(String skinPath) {
    }

    public Drawable getDrawableByName(String resName) {
        try {

            int resId = mResources.getIdentifier(resName, "drawable", mPackageName);
            Drawable drawable = mResources.getDrawable(resId);
            return drawable;
        } catch (Exception e) {

        }
        return null;
    }
    public ColorStateList getColorByName(String resName) {
        try {

            int resId = mResources.getIdentifier(resName, "color", mPackageName);
            ColorStateList colorStateList = mResources.getColorStateList(resId);
            return colorStateList;
        } catch (Exception e) {

        }
        return null;
    }
}
